An applet, a Java programme, can be added to your website. Within a web
browser, this applet runs client-side. You can host the applet on a web
server and embed it directly into an HTML page by using the object or
applet tag. This gives your website a lively and enjoyable touch.
Every applet is a subclass of the java.applet.Applet class.
Applets don't work on their own; they run inside an applet
viewer or a web browser. The applet viewer is a common tool
provided by JDK.
Unlike regular programs that start from the main() method,
applets have a different way of starting to run.
Instead of using System.out.println for displaying output in the applet window,
AWT methods like drawString() are used.
Your website can be made more interactive and interesting for your visitors by
using java applets.
Hierarchy of Applet in Java
Life
cycle of an applet
In Java applet there are 4 life cycle methods for applets in the applet class,
and 1 life cycle methods for applets in the java.awt.Component class.
java.applet.Applet
class
void init() : The init() method is the first one to be called
when your applet starts. Its main purpose is to initialize variables.
It's essential to note that this method is executed only once during the
entire runtime of your applet.
void start() : After init(), the start() method is called. It
acts as a way to restart the applet after it has been stopped. One thing
to keep in mind is that start() gets called every time the applet's HTML
document is displayed on the screen. On the other hand, init() is only
triggered once when the applet is initially loaded. This means that if a
user leaves the web page and returns, the applet resumes from the
beginning.
void stop() : When a web browser navigates away from the HTML
document containing the applet, such as when it changes to another page,
the stop() method is invoked. It is likely that the applet is running
when stop() is called. When the applet is not visible, threads that are
unnecessary to run should be suspended using the stop() function. If the
user revisits the page, you can restart them when start() is called.
void destroy() : When your applet needs to be removed entirely
from memory, the destroy() method is called by the environment. At this
point, you should release any resources that the applet might be using.
Prior to destroy(), the stop() method is always invoked.
java.awt.Component class
paint() : The Applet has been painted using paint (Graphics
Gra). It offers objects of the Graphics class, which can be used to draw
shapes like arcs, rectangles, ovals and circle
Post your comment